<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{


     // ----------------------------------------------------------评论添加
  public function postInsert(Request $request)
  {
    // dd($request->all());

        // $id = $request->only(['sid']);
    
       

        $data = $request->only(['sname','contents','uname','sid']);

        $id = $data['sid'];

        

       //添加图片
         if($request->hasFile('tupian')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('tupian')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传图片的不合法");
                    location.href="http://project.cn/detail/detail/'.$id.'";
                </script>';
            }
            $request->file('tupian')->move('./public/uploads/',$name.'.'.$ext);
            // echo '<img src="public/uploads/',$name.'.'.$ext.'" alt="">';
           
            $img = self::suofang('./public/uploads/'.$name.'.'.$ext,100,100);
            // dd($data['pic']);
            $data['tupian'] = $name.'.'.$ext;
            $time = date('Y-m-d',time());
            $data['ctime'] = $time;
          
  
            
        }


        

        // dd($id);
      //执行数据插入
      $res = DB::table('comment')->insert($data);
    
      //判断是否添加成功
      if($res){

        echo '<script type="text/javascript">
          location.href="http://project.cn/detail/detail/'.$id.'";
        </script>';
        // return redirect('home/detail')->action('DetailController@detail', $id);

        // $scenic = DB::table('scenic')->where('id',$id)->first();
        // //查询景点图片及文本
        // $photo = DB::table('scenic_photo')->where('sid',$id)->get();
        // //查询景点评论
        // $comment = DB::table('comment')->where('sid',$id)->get();

        // return view('home.detail.detail',['scenic' => $scenic,'photo'=>$photo,'comment'=>$comment]);

      }else{
        return back()->with('error','评论失败');
     
      }
    }


   


// -----------------------------------------------------评论列表
    public function getList(Request $request){
      


   
      $scenic = DB::table('scenic')->get();


   $num = 5;
   //判断是否有选择景点
   if($request->input('sid')){
     $com = DB::table('comment')->where('sid',$request->input('sid'))->paginate($num);
   }else{
     $com = DB::table('comment')->where('sid',7)->paginate($num);
   }
   //获取当前页面接收到的所有参数
   $data = $request->all();
    // var_dump($data);

   //解析模板
   return view('admin.comment.list',['com'=>$com,'request'=>$data,'scenic'=>$scenic]);
 }




// // // --------------------------------------------------------评论删除
 public  function getDelete(Request $request){
         // dd($request -> input('id'));
       $id = $request -> input('id');

       $res = DB::table('comment')->where('id',$id)->delete();
       if($res){
            echo 1;
       }else{
         echo 2;
       }
     }
  
// ----------------------------------------------------回复评论

   public function getEdit($id){
             
      $com = DB::table('comment')->where('id',$id)->first();
      
      return view('admin.comment.edit',['com'=>$com]);
    
     }


     public function postUpdate(Request $request){
       // dd($request -> all());
      $id = $request->only(['id']);

      $data = $request->only(['uname','contents','reply']);
     


        //执行修改
         $res = DB::table('comment')->where('id',$id)->update($data);
      // var_dump($res);
       if($res){
            return redirect('/admin/comment/list')->with('success','回复成功');

        }else{
          return back()->with('error','回复失败');
        }
   
     }




         //缩放图片
           public static function suofang($spath, $sw = 100, $sh = 100, $pre = 's_')
          {
          //1.创建画布
          //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
          $srcinfo = getimagesize($spath);
          //根据图片类型 去创建图像资源 ...
          $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
          
          if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
              $bili = $sh/$srcinfo[1];
          }else{
              $bili = $sw/$srcinfo[0];
          }
          //舍去法取整
          $sw = floor($srcinfo[0] * $bili);
          $sh = floor($srcinfo[1] * $bili);


          $dst = imagecreatetruecolor($sw,$sh);

          //3.开始绘画
          imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

          //5.将图像保存为文件
          //随机文件名  //获取文件后缀名  目录
          //$ext = strrchr($spath,'.');
          //$filename = time().rand(1000,9999).$ext;
          $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
          
          imagejpeg($dst, $filename);

          //6.销毁资源
          imagedestroy($dst);
          imagedestroy($src);
          
          //返回原文件名加前缀
          return $pre.basename($spath);
      }

}
